﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using SMS.WinForms;

namespace SMS.Console.Controls
{
    public partial class WWFConfigPage : ConfigPage
    {
        XmlNode connNode = null;
        SqlConnectionStringBuilder connBuilder = null;
        public WWFConfigPage()
        {
            InitializeComponent();
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //读出 /configuration/OneFlowRuntime/CommonParameters/add[@key='ConnectionString']
            connNode = this.Document.SelectSingleNode("/configuration/OneFlowRuntime/CommonParameters/add[@name='ConnectionString']");
            //对文本框赋值
            if (connNode != null)
            {
                XmlAttribute valueAttr = connNode.Attributes["value"];
                if (valueAttr!=null)
                {
                    connBuilder = new SqlConnectionStringBuilder(valueAttr.Value);
                    cmb_InitialCatalog.Text = connBuilder.InitialCatalog;
                    txt_DataSource.Text = connBuilder.DataSource;
                    cmb_ValidateType.SelectedIndex = connBuilder.IntegratedSecurity ? 0 : 1;
                    pnl_UserPassword.Enabled = !connBuilder.IntegratedSecurity;
                    txt_UserName.Text = connBuilder.UserID;
                    txt_Password.Text = connBuilder.Password;
                }
            }
        }

        public override bool CanNavigate(SMS.WinForms.WizardFormNavigationEventArgs e)
        {
            if (e.NavigationType == WizardFormNavigationTypes.Next)
            {
                try
                {
                    string connStr = BuildConnString();
                    SetConnNodeValue(connStr);
                }
                catch (Exception ex)
                {
                    ErrorBox.Show(ex);
                    return false;
                }
            }
            return true;
        }

        private string BuildConnString()
        {
            if (connBuilder == null) connBuilder = new SqlConnectionStringBuilder();
            connBuilder.DataSource = txt_DataSource.Text;
            connBuilder.InitialCatalog = cmb_InitialCatalog.Text;
            bool integratedSecurity = cmb_ValidateType.SelectedIndex == 0;
            connBuilder.IntegratedSecurity = integratedSecurity;
            if (!integratedSecurity)
            {
                connBuilder.UserID = txt_UserName.Text;
                connBuilder.Password = txt_Password.Text;
            }
            string connStr = connBuilder.ToString();
            return connStr;
        }

        private void cmb_ValidateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnl_UserPassword.Enabled = cmb_ValidateType.SelectedIndex == 1;
        }
        private void SetConnNodeValue(string connstr)
        {
            if (connNode == null)
            {
                XmlNode configurationNode = XmlHelper.LoadNode(this.Document, "configuration");
                XmlNode oneFlowRuntimeNode = XmlHelper.LoadNode(configurationNode, "OneFlowRuntime");
                XmlNode commonParametersNode = XmlHelper.LoadNode(oneFlowRuntimeNode, "CommonParameters");
                connNode = XmlHelper.LoadNode(commonParametersNode, "add");
                XmlAttribute keyAttr = this.Document.CreateAttribute("name");
                keyAttr.Value = "ConnectionString";
                connNode.Attributes.Append(keyAttr);
            }
            XmlAttribute xAttr = connNode.Attributes["value"];
            if (xAttr != null)
            {
                xAttr.Value = connstr;
            }
            else
            {
                XmlAttribute valueAttr = this.Document.CreateAttribute("value");
                valueAttr.Value = connstr;
                connNode.Attributes.Append(valueAttr);
            }
        }

        private void btn_Test_ConnectionStringRequest(object sender, ConnectionTestButton.ConnectionStringRequestArgs e)
        {
            e.ConnectionString = BuildConnString();

        }
    }
}
